﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using rehabilitation.Data;
using rehabilitation.Models;

namespace WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CommunicateController : ControllerBase
    {
        private readonly AppDbContext _context;

        public CommunicateController(AppDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 1. 根据医生ID查询医患沟通列表（分页）
        /// </summary>
        [HttpGet("listByDoctor/{doctorId}")]
        public async Task<IActionResult> GetCommunicateByDoctor(int doctorId, int pageIndex = 1, int pageSize = 10)
        {
            if (pageIndex < 1 || pageSize < 1)
            {
                return BadRequest("分页参数无效");
            }

            var query = _context.Communicates
                .Where(c => c.DoctorId == doctorId)
                .OrderByDescending(c => c.Time);

            int totalRecords = await query.CountAsync();
            var communicateList = await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();

            return Ok(new
            {
                TotalRecords = totalRecords,
                PageIndex = pageIndex,
                PageSize = pageSize,
                Data = communicateList
            });
        }

        /// <summary>
        /// 2. 根据ID查询医患沟通详情
        /// </summary>
        [HttpGet("{id}")]
        public async Task<IActionResult> GetCommunicateById(int id)
        {
            var communicate = await _context.Communicates.FindAsync(id);
            if (communicate == null)
            {
                return NotFound("沟通记录不存在");
            }
            return Ok(communicate);
        }

        /// <summary>
        /// 3. 编辑医患沟通记录
        /// </summary>
        [HttpPut("update")]
        public async Task<IActionResult> UpdateCommunicate([FromBody] Communicate communicate)
        {
            var existingCommunicate = await _context.Communicates.FindAsync(communicate.Id);
            if (existingCommunicate == null)
            {
                return NotFound("沟通记录不存在");
            }

            existingCommunicate.Level = communicate.Level;
            existingCommunicate.Opinion = communicate.Opinion;
            existingCommunicate.Time = communicate.Time;

            await _context.SaveChangesAsync();
            return Ok("医患沟通记录已更新");
        }

        /// <summary>
        /// 4. 新增医患沟通记录
        /// </summary>
        [HttpPost("add")]
        public async Task<IActionResult> AddCommunicate([FromBody] Communicate communicate)
        {
            _context.Communicates.Add(communicate);
            await _context.SaveChangesAsync();
            return Ok("医患沟通记录已新增");
        }

        /// <summary>
        /// 5. 删除医患沟通记录
        /// </summary>
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteCommunicate(int id)
        {
            var communicate = await _context.Communicates.FindAsync(id);
            if (communicate == null)
            {
                return NotFound("沟通记录不存在");
            }

            _context.Communicates.Remove(communicate);
            await _context.SaveChangesAsync();
            return Ok("医患沟通记录已删除");
        }
    }
}
